<?php
/**
 * Description of FriendController
 *
 * @author TheFaSTeR
 */

Yii::import("application.controllers.FrontendBaseController");

class FriendController extends FrontendBaseController
{            
    public function actionIndex( $id = null )
    {
        if ($id == null)
        {
            $id = $this->getUserIdFromSession();
        }
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Друзья" => "" );
        
        $user = $this->loadModel($id);
        
        $isAuthor = $user->user_id == Yii::app()->user->id ? true : false;
        
        $friends = FriendFacade::getFriendsByUserId($user->user_id);                
        
        if ($isAuthor)
        {
            $waitFriends = FriendFacade::getWaitFriendsByUserId($user->user_id);            
        }

        $this->render('friend', array(
            'isAuthor'      => $isAuthor,
            'friends'       => $friends,
            'waitFriends'   => $waitFriends
            ));                
    }           
    
    public function actionAdd( $id )
    {
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        $user = $this->loadModel($id);
        $friend = new Friend;
        $friend->friend_list_id = FriendFacade::getFriendList(Yii::app()->user->id)->list_id;
        $friend->user_id = $user->user_id;
        $friend->accepted = 0;
        $friend->save();
        
        $this->redirect($this->redirectToUserPage());
    }
    
    public function actionDelete( $id )
    {        
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }                                
        $user = $this->loadModel($id);
        
        $firstFriendId = FriendFacade::getFriendId(Yii::app()->user->id, $user->user_id);
        $secondFriendId = FriendFacade::getFriendId($user->user_id, Yii::app()->user->id);                
        
        if (!$firstFriendId || !$secondFriendId)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        $firstFriendId->delete();
        $secondFriendId->delete();                        
        
        $this->redirect($this->redirectToUserPage());
    }
    
    public function actionAccept( $id )
    {
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        $user = $this->loadModel($id);
        
        $friend = new Friend;
        $friend->friend_list_id = FriendFacade::getFriendList(Yii::app()->user->id)->list_id;
        $friend->user_id = $user->user_id;
        $friend->accepted = 1;
        $friend->save();
        
        $friendId = FriendFacade::getFriendId($user->user_id, Yii::app()->user->id);
        $friendId->accepted = 1;
        $friendId->save();
        
        $this->redirect("/friend");
    }
    
    public function loadModel( $id )
    {
        $model = UserFacade::getUserById( $id );
        if ( $model === null )
            throw new CHttpException( 404, 'The requested page does not exist.' );
        return $model;
    }    

}
?>
